<template>
  <div class="content">
    <a-form ref="formRef" :model="formState" :rules="rules">
      <a-form-item ref="oldpwd" label="输入旧密码" name="oldpwd">
        <a-input
          v-model:value="formState.oldpwd"
          type="password"
          placeholder="请输入旧密码"
        />
      </a-form-item>

      <a-form-item ref="newpwd" label="输入新密码" name="newpwd">
        <a-input
          v-model:value="formState.newpwd"
          type="password"
          placeholder="请输入密码"
        />
      </a-form-item>

      <a-form-item ref="newpwdone" label="确认新密码" name="newpwdone">
        <a-input
          v-model:value="formState.newpwdone"
          type="password"
          placeholder="请再次确认密码"
        />
      </a-form-item>

      <a-form-item>
        <a-button type="primary" @click="onSubmit" class="button"
          >提 交</a-button
        >
      </a-form-item>
    </a-form>
  </div>
</template>

<script>
import { defineComponent, reactive, ref } from "vue";
import { message } from "ant-design-vue";
import { setUpPwd } from "@/api/https.js";
export default defineComponent({
  setup() {
    const formRef = ref();
    const formState = reactive({
      user_id: JSON.parse(localStorage.getItem("info")).user_id,
      oldpwd: "", // 输入旧密码
      newpwd: "", // 输入新密码
      newpwdone: "", //确认新密码
    });
    // 二次密码验证
    let passwordZZ = async () => {
      if (formState.newpwd !== formState.newpwdone) {
        return Promise.reject("两次密码输入不一致");
      } else {
        return Promise.resolve();
      }
    };
    const rules = {
      oldpwd: [
        {
          required: true,
          message: "请输入旧密码",
          trigger: "blur",
        },
        {
          min: 6,
          max: 13,
          message: "请填写6-13位密码",
          trigger: "blur",
        },
      ],
      newpwd: [
        {
          required: true,
          message: "请输入新密码",
          trigger: "blur",
        },
        {
          min: 6,
          max: 13,
          message: "请填写6-13位密码",
          trigger: "blur",
        },
      ],
      newpwdone: [
        {
          required: true,
          message: "请确认新密码",
          trigger: "blur",
        },
        {
          validator: passwordZZ,
          trigger: "blur",
        },
      ],
    };

    const onSubmit = async () => {
      formRef.value
        .validate()
        .then(async () => {
          const data = await setUpPwd(formState);
          if (data.code == 1) {
            message.success(`${data.msg}`);
            formState.value = reactive({
              user_id: JSON.parse(localStorage.getItem("info")).user_id,
              oldpwd: "", // 输入旧密码
              newpwd: "", // 输入新密码
              newpwdone: "", //确认新密码
            });
          } else {
            message.error(`${data.msg}`);
          }
        })
        .catch((error) => {
          console.log("error", error);
        });
    };

    return {
      formRef,
      formState,
      rules,
      onSubmit,
    };
  },
});
</script>

<style lang="less" scoped>
.content {
  position: relative;
  left: 50%;
  transform: translateX(-50%);
  max-width: 500px;
  margin-top: 40px;

  .ant-form-item {
    flex-direction: var(--bannerFlex);
    justify-content: center;
  }

  .button {
    margin-top: 20px;
    width: 100%;
    height: 45px;
    background: #554de6;
    border-radius: 25px;
    font-size: var(--txtSize);
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #ffffff;
  }

  .buttons {
    text-align: center;
    font-size: var(--txtSize);
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #808080;
    cursor: pointer;
  }

  /deep/ .ant-col {
    width: 300px;
  }
  /deep/ .ant-form-item-label {
    width: 140px;
    margin-right: 20px;
  }
}
</style>
